package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.BookPageRequest;
import com.example.springboot.controller.request.BorrowPageRequest;
import com.example.springboot.entity.Borrow;
import com.example.springboot.entity.revert;
import com.example.springboot.service.BookService;
import com.example.springboot.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private BorrowService  borrowService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Borrow> list = borrowService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/page")
    public Result page(BorrowPageRequest borrowPageRequest){
        return Result.success(borrowService.page(borrowPageRequest));
    }
    /**
     * 新增用户
     * 前端如何把数据发送给给后台：是通过HTTP的方式，通过POST请求，把JSON数据发送给后台接口，后台接口接收到数据之后，转换成JAVA的对象，然后把Java对象，存到数据库中。
     */
    @PostMapping("/save")
    public Result save(@RequestBody Borrow obj){
        borrowService.save(obj);
        return Result.success();
    }
    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Borrow obj = borrowService.getById(id);
        return Result.success(obj);
    }

    /**
     * 修改数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody Borrow obj){
        borrowService.update(obj);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        borrowService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/pageRevert")
    public Result pageRevert(BorrowPageRequest borrowPageRequest){
        return Result.success(borrowService.pageRevert(borrowPageRequest));
    }

    @PostMapping("/saveRevert")
    public Result saveRevert(@RequestBody revert obj){
        borrowService.saveRevert(obj);
        return Result.success();
    }
    @DeleteMapping("/deleteRevert/{id}")
    public Result deleteRevert(@PathVariable Integer id){
        borrowService.deleteRevertById(id);
        return Result.success();
    }

}
